package com.chunuo.blogs.config;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 配置类
 *
 * @author liujiamiao
 * @email 2544296058@qq.com
 * @date 2020-05-05 20:07
 */
@Slf4j
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/test.html").setViewName("test");
        registry.addViewController("/index.html").setViewName("index");
        registry.addViewController("/login.html").setViewName("login");
        registry.addViewController("/details.html").setViewName("details");
        registry.addViewController("/classify.html").setViewName("classify");
        registry.addViewController("/tag.html").setViewName("tag");
        registry.addViewController("/filing.html").setViewName("filing");
        registry.addViewController("/admin/indexList.html").setViewName("admin/indexList");
        registry.addViewController("/admin/indexPublish.html").setViewName("admin/indexPublish");
        registry.addViewController("/admin/classifyList.html").setViewName("admin/classifyList");
        registry.addViewController("/admin/classifyPublish.html").setViewName("admin/classifyPublish");
        registry.addViewController("/admin/tagList.html").setViewName("admin/tagList");
        registry.addViewController("/admin/tagPublish.html").setViewName("admin/tagPublish");
        registry.addViewController("/admin/login.html").setViewName("admin/login");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

    //日期多种格式
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new CustomDateConverter());
    }

    //定义时间输出格式（默认并非我们想要的）
    @Bean
    public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        converter.setObjectMapper(mapper);
        return converter;
    }
    //添加转换器
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //将我们定义的时间格式转换器添加到转换器列表中,
        //这样jackson格式化时候但凡遇到Date类型就会转换成我们定义的格式
        converters.add(jackson2HttpMessageConverter());
    }


    /**
     *
     * addMapping("/**") 对接口配置跨域设置 /**代表所有接口
     * allowedHeaders("*") 允许所有的请求头
     * allowedMethods("*") 允许所有的方法 也可以设置为("POST", "GET", "PUT", "OPTIONS", "DELETE")
     * allowedOrigins("*") 允许所有的域（源地址）
     * @return
     */
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setAllowCredentials(true);
        return corsConfiguration;
    }
    /*
     * 跨域过滤器
     * @return
     */
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }



}
